package com.lg.baselibrary.utils;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

/**
 * Created by ligang on 2017/5/31.
 * description：
 * 适用场景：EditText右边有个清空按钮的情况
 */

public class EditTextClearHandler implements TextWatcher, View.OnClickListener {
    private EditText editText;
    private View clearView;
    public void init(EditText editText, View clearView){
        this.editText = editText;
        this.clearView = clearView;
        editText.addTextChangedListener(this);
        clearView.setOnClickListener(this);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if(TextUtils.isEmpty(s)){
            clearView.setVisibility(View.GONE);
        }else{
            if(clearView.getVisibility() != View.VISIBLE){
                clearView.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public void onClick(View v) {
        editText.getText().clear();
    }
}
